Example 1. Authorizing an application to access Twitter account data


In [1]:
import twitter
# Go to http://dev.twitter.com/apps/new to create an app and get values
# for these credentials, which you'll need to provide in place of these
# empty string values that are defined as placeholders.
# See https://dev.twitter.com/docs/auth/oauth for more information 
# on Twitter's OAuth implementation.

CONSUMER_KEY = 'Tx8GozuzaraJO89LypsTqFUNM'
CONSUMER_SECRET ='uC0KPaRZjGY0qoNJAxBQb0jbb3Us4DXJVKFCP1qtSfjFgbRvd0'
OAUTH_TOKEN = '571213367-BC7MYnWpLbvy2AYhvqCu6Fh1EIeFasPxo0Y8uBEn'
OAUTH_TOKEN_SECRET = 'KA9GddiLbVwummLoSGmDYMNQFVaR3P4YiGo7Zs6D5e7Qz'

auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
                           CONSUMER_KEY, CONSUMER_SECRET)

twitter_api = twitter.Twitter(auth=auth)

# Nothing to see by displaying twitter_api except that it's now a
# defined variable

print twitter_api


<twitter.api.Twitter object at 0x000000000426F240>

Example 2. Retrieving trends


In [2]:
# The Yahoo! Where On Earth ID for the entire world is 1.
# See https://dev.twitter.com/docs/api/1.1/get/trends/place and
# http://developer.yahoo.com/geo/geoplanet/

WORLD_WOE_ID = 1
US_WOE_ID = 23424977

# Prefix ID with the underscore for query string parameterization.
# Without the underscore, the twitter package appends the ID value
# to the URL itself as a special case keyword argument.

world_trends = twitter_api.trends.place(_id=WORLD_WOE_ID)
us_trends = twitter_api.trends.place(_id=US_WOE_ID)

Example 3. Reading data into MongoDB


In [3]:
import json
import pymongo

In [4]:
# Based upon example 9-7 in *required reading*
# Mining the Soocial Web, Chapter 9

# Connects to the MongoDB server running on 
# localhost:27017 by default
    
client = pymongo.MongoClient()
    
# Get a reference to a particular database
    
db = client['twitter']
    
# Reference a particular collection in the database
coll = db['us_trends']

# Clear any old data out of the database
# **Only for Demonstration**
coll.drop()
    
# Perform a bulk insert and  return the IDs
    
_ = coll.insert(us_trends[0]['trends'])

Example 4. Reading data out of MongoDB


In [5]:
# Connects to the MongoDB server running on 
# localhost:27017 by default
    
client = pymongo.MongoClient()
    
# Get a reference to a particular database
    
db = client['twitter']
    
# Reference a particular collection in the database
coll = db['us_trends']

In [6]:
cursor = coll.find({})

In [7]:
for trend in cursor:
    print trend['name'], trend['tweet_volume']


#Trumpertantrum 26665
#HillarySoProgressive 23859
#EVOL 32249
#Madoff None
#MyIdeaOfQualityTimeIs None
Rick Santorum 24510
Thug 99725
Drita None
Sumner Redstone None
Carmella None
PJ Black None
Valencia 299638
Alerta Roja None
US Soccer None
#DownloadThisIsNotTheAlbum 17829
#AwkwardYearbookSignings None
#YoungandHungry None
#BetterWaysToElectPOTUS 15143
#WizWarriors None
#MosqueVisit 60876
#DownInTheDMRemix None
#WelcomeBackWTK None
#DemBows None
#MysteriesofLaura None
#ThatsWhenIHeardTheSirens None
#doggydatenight None
#CruzisaSnakeBecause None
#YolandahTODAY None
#pugchat None
#HayQueSalirDeMaduro 22533
#SELFIEFORSEB 35757
#WhoWillSpeakForEngland 15648
#BillCosby None
#PleaseClap None
#trollingstone None
#createlounge None
#BlueSteelSelfie None
#CLEatCHA None
#DETvsTBL None
#eCreameryLove None
#1045BdayShow None
#TackleHomelessness None
#GSWatWAS None
#innovateNJ None
#asktristan None
#Ask5SOS None
#livingroomsession 35849
#SoloRound None
#WhenILoseIBlame None
#RDTV None

Example 5. Searching MongoDB


In [8]:
# Do a search!  See 
# https://docs.mongodb.org/getting-started/python/query/
# and
# https://docs.mongodb.org/manual/tutorial/query-documents/
# for details.
cursor = coll.find({'tweet_volume': {'$gt': 30000} })

In [9]:
for trend in cursor:
    print trend['name'], trend['tweet_volume']


#EVOL 32249
Thug 99725
Valencia 299638
#MosqueVisit 60876
#SELFIEFORSEB 35757
#livingroomsession 35849

Example 6. Fancy searches... regular expressions!


In [10]:
# Do a search!  See 
# https://docs.mongodb.org/getting-started/python/query/
# and
# https://docs.mongodb.org/manual/tutorial/query-documents/
# for details.
cursor = coll.find({'$or': [ {'tweet_volume': {'$gt': 30000}} , {'name':  {'$regex': '.*love.*', '$options': 'i' }} ] })

In [11]:
for trend in cursor:
    print trend['name'], trend['tweet_volume']


#EVOL 32249
Thug 99725
Valencia 299638
#MosqueVisit 60876
#SELFIEFORSEB 35757
#eCreameryLove None
#livingroomsession 35849

In [ ]:


In [ ]: